package splash;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import coganh.Activity.R;

public class SplashThread extends Thread {

	public int STATE_RUNNING = 4;

	private Bitmap b;

	private ArrayList<SplashSprite> mSprites;
	private ArrayList<SplashSprite> mSpritestoRemove;

	private int mMode;

	private long mLastTime;

	private boolean mRun = false;

	private int frameSamplesCollected = 0;
	private int frameSampleTime = 0;
	@SuppressWarnings("unused")
	private int fps = 0;

	private SurfaceHolder mSurfaceHolder;

	private Resources res;

	public SplashThread(SurfaceHolder surfaceHolder, Context context, AttributeSet attrs) {
		// get handles to some important objects
		mSurfaceHolder = surfaceHolder;
		res = context.getResources();

		mSprites = new ArrayList<SplashSprite>();
		mSpritestoRemove = new ArrayList<SplashSprite>();
	}

	@Override
	public void run() {
		SplashSprite a = new SplashSprite();

		if (b == null)
			b = BitmapFactory.decodeResource(res, R.drawable.explosion);

		a.Initialize(b, 120, 160, 24, 20, true);

		a.setxPost(150);
		a.setyPost(300);

		synchronized (mSprites) {
			mSprites.add(a);
		}

		while (mRun) {
			Canvas c = null;
			try {
				c = mSurfaceHolder.lockCanvas(null);
				synchronized (mSurfaceHolder) {
					if (mMode == STATE_RUNNING)
						updatePhysics();
					doDraw(c);
				}
			} catch (Exception e) {
				System.out.println(e.getStackTrace());
			} finally {
				if (c != null) {
					mSurfaceHolder.unlockCanvasAndPost(c);
				}
			}
		}
	}

	/**
	 * Draws to the provided Canvas.
	 */
	private void doDraw(Canvas canvas) {
		canvas.drawColor(Color.BLACK);

		for (SplashSprite a : mSprites) {
			a.draw(canvas);
		}

		canvas.restore();
	}

	private void updatePhysics() {
		long now = System.currentTimeMillis();

		if (mLastTime > now)
			return;

		if (mLastTime != 0) {
			int time = (int) (now - mLastTime);
			frameSampleTime += time;
			frameSamplesCollected++;
			if (frameSamplesCollected == 10) {
				fps = (int) (10000 / frameSampleTime);
				frameSampleTime = 0;
				frameSamplesCollected = 0;
			}
		}

		synchronized (mSprites) {
			for (SplashSprite a : mSprites) {
				a.Update(now);

				if (a.dispose)
					mSpritestoRemove.add(a);
			}
		}

		synchronized (mSpritestoRemove) {
			mSprites.removeAll(mSpritestoRemove);
			mSpritestoRemove.clear();
		}

		mLastTime = now;
	}

	public boolean ismRun() {
		return mRun;
	}

	public void setmRun(boolean mRun) {
		this.mRun = mRun;
	}

	public void setRunning(boolean b) {
		mRun = b;
	}

	public void setState(int mode) {
		synchronized (mSurfaceHolder) {
			this.mMode = mode;
		}
	}

	public void doTouch(MotionEvent event) {
		// TODO Auto-generated method stub
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			this.clearSprite();
			this.setRunning(false);
		}
	}

	public void clearSprite() {
		synchronized (mSprites) {
			this.mSprites.clear();
		}
	}
}
